#include "Gui.h"

using namespace Ogre;
using namespace std;

Gui::~Gui(void){
}
Gui::Gui(Ogre::Root *mRoot, MapEditor *mapE){

	mapEditor = mapE;

    CEGUI::OgreCEGUIRenderer *mRenderer;
    CEGUI::System *mSystem;

	// CEGUI setup
	mRenderer = new CEGUI::OgreCEGUIRenderer(mRoot->getAutoCreatedWindow(), Ogre::RENDER_QUEUE_OVERLAY, false, 3000, mRoot->getSceneManager("Default SceneManager"));
	mSystem = new CEGUI::System(mRenderer);
	CEGUI::SchemeManager::getSingleton().loadScheme((CEGUI::utf8*)"TaharezLookSkin.scheme");
	mSystem->setDefaultMouseCursor((CEGUI::utf8*)"TaharezLook", (CEGUI::utf8*)"MouseArrow");
	mSystem->setDefaultFont((CEGUI::utf8*)"BlueHighway-12");
	CEGUI::MouseCursor::getSingleton().setImage(CEGUI::System::getSingleton().getDefaultMouseCursor());

	CEGUI::WindowManager *win = CEGUI::WindowManager::getSingletonPtr();
	//CEGUI::Window *sheet = win->createWindow("DefaultGUISheet", "Editor/Sheet");
	CEGUI::Window* sheet = 
            CEGUI::WindowManager::getSingleton().loadWindowLayout(
                (CEGUI::utf8*)"editorgui.layout"); 

	mSystem->setGUISheet(sheet);

    CEGUI::Combobox* objectComboBox = (CEGUI::Combobox*)CEGUI::WindowManager::getSingleton().getWindow("EditorGui/TabCtrl/Page2/ObjectTypeList");

	fillList(objectComboBox,"..\\..\\media\\models");

	win->getWindow((CEGUI::utf8*)"EditorGui/TabCtrl/MainPage/QuitButton")
		->subscribeEvent(
			CEGUI::PushButton::EventClicked, 
			CEGUI::Event::Subscriber(&Gui::handleQuit, this));

	win->getWindow((CEGUI::utf8*)"EditorGui/TabCtrl/MainPage/LoadButton")
		->subscribeEvent(
			CEGUI::PushButton::EventClicked, 
			CEGUI::Event::Subscriber(&Gui::loadModelMenu, this));
	
    win->getWindow((CEGUI::utf8*)"EditorGui/TabCtrl/Page2/ObjectTypeList")
		->subscribeEvent(
			CEGUI::Combobox::EventListSelectionAccepted, 
			CEGUI::Event::Subscriber(&Gui::loadModelMenu, this));

    win->getWindow((CEGUI::utf8*)"EditorGui/TabCtrl/Page3/Remove")
		->subscribeEvent(
			CEGUI::PushButton::EventClicked, 
			CEGUI::Event::Subscriber(&Gui::removeModel, this));

    win->getWindow((CEGUI::utf8*)"EditorGui/TabCtrl/Page3/Scale")
		->subscribeEvent(
			CEGUI::PushButton::EventClicked, 
			CEGUI::Event::Subscriber(&Gui::scaleModel, this));

    win->getWindow((CEGUI::utf8*)"EditorGui/TabCtrl/Page3/Rotate")
		->subscribeEvent(
			CEGUI::PushButton::EventClicked, 
			CEGUI::Event::Subscriber(&Gui::rotateModel, this));

    win->getWindow((CEGUI::utf8*)"EditorGui/TabCtrl/Page3/Duplicate")
		->subscribeEvent(
			CEGUI::PushButton::EventClicked, 
			CEGUI::Event::Subscriber(&Gui::duplicateModel, this));

    win->getWindow((CEGUI::utf8*)"EditorGui/TabCtrl/Page3/Move")
		->subscribeEvent(
			CEGUI::PushButton::EventClicked, 
			CEGUI::Event::Subscriber(&Gui::moveModel, this));
	
}

void Gui::fillList(CEGUI::Combobox* objectComboBox, string _path){

	HANDLE fh;
    CEGUI::ListboxTextItem* item;
	int i=0;
	int    filecnt=0;
	char	 path[MAX_PATH];
	char	 tmppath[MAX_PATH];

	WIN32_FIND_DATA *fd;
	fd = (WIN32_FIND_DATA*) malloc(sizeof(WIN32_FIND_DATA));

	fixpath(_path.c_str(),path);
	strcat(path,"*");

	fh = FindFirstFile((LPCSTR) path,fd);

	if(fh != INVALID_HANDLE_VALUE)
	{
		do
		{
			filecnt++;
			if(fd->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
			{

				if((0 != strcmp(fd->cFileName,".")) && (0 != strcmp(fd->cFileName,"..")))
				{
					fixpath(_path.c_str(),tmppath);
					strcat(tmppath,fd->cFileName);
					fixpath(tmppath,tmppath);
					item = new CEGUI::ListboxTextItem((CEGUI::utf8*)fd->cFileName, i);
					objectComboBox->addItem(item);
					i++;
				}
			}
			else
			{

				item = new CEGUI::ListboxTextItem((CEGUI::utf8*)fd->cFileName, i);
				objectComboBox->addItem(item);
				i++;
			}


		}
		while(FindNextFile(fh,fd));
	}

	FindClose(fh);

}


//------------------------------------------------------------------------------------------------------------------
// fixpath() - Adds \ to the end of a path if not present.
//------------------------------------------------------------------------------------------------------------------
int Gui::fixpath(const char *inpath, char *outpath)
{
  int   n=0;

  strcpy(outpath,inpath);

  while(inpath[n]) n++;

  if(inpath[n-1] != '\\')
  {
    strcat(outpath,"\\");
    return 1;
  }

  return 0;
}

void Gui::setSheetVisible(bool b){
	
	CEGUI::WindowManager *win = CEGUI::WindowManager::getSingletonPtr();
	CEGUI::Window* sheet = win->getWindow((CEGUI::utf8*)"root");
	sheet->setVisible(b);
}

void Gui::setMouseVisible(bool b){
	
	CEGUI::MouseCursor::getSingleton().setVisible(b);
}

void Gui::centerMouse(){
	
	CEGUI::MouseCursor::getSingleton().setPosition(CEGUI::Point::Vector2(500,500));
}

bool Gui::handleQuit(const CEGUI::EventArgs& e){
	
	mapEditor->quit();
    return true;
}

bool Gui::loadModelMenu(const CEGUI::EventArgs& e){

    const CEGUI::WindowEventArgs& windowEventArgs = static_cast<const CEGUI::WindowEventArgs&>(e);
    CEGUI::ListboxItem* item = static_cast<CEGUI::Combobox*>(windowEventArgs.window)->getSelectedItem();
	mapEditor->addModel(item->getText());
	return true;
}

bool Gui::removeModel(const CEGUI::EventArgs& e){

	mapEditor->startRemovingObject();
	return true;
}

bool Gui::scaleModel(const CEGUI::EventArgs& e){

	mapEditor->startScalingObject();
	return true;
}

bool Gui::rotateModel(const CEGUI::EventArgs& e){

	mapEditor->startRotatingObject();
	return true;
}

bool Gui::duplicateModel(const CEGUI::EventArgs& e){

	mapEditor->startDuplicatingObject();
	return true;
}

bool Gui::moveModel(const CEGUI::EventArgs& e){

	mapEditor->startMovingObject();
	return true;
}